package com.heima.reggie.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.reggie.common.Thutil;
import com.heima.reggie.common.deleteException;
import com.heima.reggie.mapper.OrdersMapper;
import com.heima.reggie.pojo.AddressBook;
import com.heima.reggie.pojo.OrderDetail;
import com.heima.reggie.pojo.Orders;
import com.heima.reggie.pojo.ShoppingCart;
import com.heima.reggie.service.OrderDetailService;
import com.heima.reggie.service.OrdersService;
import com.heima.reggie.service.ShoppingCarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
@Transactional
@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements OrdersService {
 @Autowired
    private ShoppingCarService shoppingCarService;
@Autowired
 private AddressBookServiceImpl addressBookService;
@Autowired
private OrderDetailService orderDetailService;

    @Override
    public void submit2(Orders orders) {
        Long addressBookId = orders.getAddressBookId();
        Long User = Thutil.getThreadLocal();
        String remark = orders.getRemark();
        LambdaQueryWrapper<ShoppingCart> sp = new LambdaQueryWrapper<>();
        sp.eq(ShoppingCart::getUserId,User);
    List<ShoppingCart> list = shoppingCarService.list(sp);
        if (list == null) {
            throw new deleteException("购物车为空,无法结算");
        }
        AddressBook byId = addressBookService.getById(addressBookId);

        if (byId == null) {
            throw new deleteException("地址异常,无法结算");
        }
        long orderId = IdWorker.getId();//订单号

        AtomicInteger amount = new AtomicInteger(0);

        //组装订单明细信息
        List<OrderDetail> orderDetails = list.stream().map((item) -> {
            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setOrderId(orderId);
            orderDetail.setNumber(item.getNumber());
            orderDetail.setDishFlavor(item.getDishFlavor());
            orderDetail.setDishId(item.getDishId());
            orderDetail.setSetmealId(item.getSetmealId());
            orderDetail.setName(item.getName());
            orderDetail.setImage(item.getImage());
            orderDetail.setAmount(item.getAmount());

            amount.addAndGet(item.getAmount().multiply(new BigDecimal(item.getNumber())).intValue());
            return orderDetail;
        }).collect(Collectors.toList());
//组装订单数据
        orders.setId(orderId);
        orders.setOrderTime(LocalDateTime.now());
        orders.setCheckoutTime(LocalDateTime.now());
        orders.setStatus(2);
        orders.setAmount(new BigDecimal(amount.get()));//总金额
        orders.setUserId(User);
        orders.setNumber(String.valueOf(orderId));
     //   orders.setUserName(user.getName());
        orders.setConsignee(byId.getConsignee());
        orders.setPhone(byId.getPhone());
        orders.setAddress((byId.getProvinceName() == null ? "" : byId.getProvinceName())
                + (byId.getCityName() == null ? "" : byId.getCityName())
                + (byId.getDistrictName() == null ? "" : byId.getDistrictName())
                + (byId.getDetail() == null ? "" : byId.getDetail()));
        //向订单表插入数据，一条数据
        this.save(orders);

        //向订单明细表插入数据，多条数据
        orderDetailService.saveBatch(orderDetails);

        //清空购物车数据
    shoppingCarService.remove(sp);
    }
    }

